package com.bci.internal.engine.bcibpmengine.service.impl;



import com.bci.internal.engine.bcibpmengine.service.ActivitiDefinitionService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author jzwu
 * @since 2021/4/5 0005
 */
@Slf4j
@Service
public class ActivitiDefinitionServiceImpl implements ActivitiDefinitionService {

    @Resource
    private RepositoryService repositoryService;

    @Override
    public void getDefinitionXML(HttpServletResponse response, String deploymentId, String resourceName) {
        InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, resourceName);
        int count = 0;
        try {
            count = inputStream.available();
            byte[] bytes = new byte[count];
            response.setContentType("text/xml");
            OutputStream outputStream = response.getOutputStream();
            while (inputStream.read(bytes) != -1) {
                outputStream.write(bytes);
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
}